package boardgamer.yavalath.client.mainmenu;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.RectF;
import android.view.MotionEvent;
import boardgamer.common.client.HasEventHandler;
import boardgamer.common.client.animation.AnimationCollection;
import boardgamer.common.client.animation.Screen;
import boardgamer.common.client.layout.LayoutGroup;
import boardgamer.yavalath.client.MainView;
import boardgamer.yavalath.client.PlayerButton;
import boardgamer.yavalath.client.Settings;
import boardgamer.yavalath.client.SharedObjects;
import boardgamer.yavalath.client.Version;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * @author garysoed
 */
public class MainMenuScreen implements Screen {

  private static final float MENU_BUTTON_WIDTH_RATIO = 0.7f;
  private static final float MENU_BUTTON_BOTTOM_RATIO = 0.85f;
  private static final float MENU_BUTTON_GAP_HEIGHT_RATIO = 0.5f;

  private Title title;
  private PlayerButton playerButton[] = new PlayerButton[3];

  // New Menu
  private final AnimationCollection animations;
  private MainMenuButton localGameButton;
  private MainMenuButton multiplayerButton;
  private MainMenuButton replayButton;
  private MenuPanel menuPanel;

  private final MainView gameView;

  public MainMenuScreen(MainView gameView) {
    this.gameView = checkNotNull(gameView, "view");

    animations = new AnimationCollection();
  }

  @Override
  public void initialize(Rect canvasRect) {
    initializeNewMenu(canvasRect);
  }

  @Override
  public void onOpen() { }

  @Override
  public void onResume() { }

  private void initializeNewMenu(Rect canvasRect) {
    title = new Title();
    LayoutGroup titleGroup = new LayoutGroup()
        .add(title).atCurrentRect();
    titleGroup.setBoundingRect(new RectF(canvasRect));
    titleGroup.offsetTo(0, 0);

    menuPanel = new MenuPanel(Color.RED, canvasRect);
    
    localGameButton = new MainMenuButton("Local Game",
        new HasEventHandler.EventHandler() {
          @Override
          public void onEvent(float eventX, float eventY, int eventAction) {
            gameView.showLocalGameLobby();
          }
        }, false /* proVersion */);

    if (Version.MULTIPLAYER.isEnabled()) {
      multiplayerButton = new MainMenuButton("Multiplayer",
          new HasEventHandler.EventHandler() {
            @Override
            public void onEvent(float eventX, float eventY, int eventAction) {
              SharedObjects.instance().getProFeatures().startMultiplayerView();
            }
          }, true /* proVersion */);
    }
    replayButton = new MainMenuButton("Replay",
        new HasEventHandler.EventHandler() {
          @Override
          public void onEvent(float eventX, float eventY, int eventAction) {
            SharedObjects.instance().getProFeatures().startReplayLobbyView();
          }
        }, true /* proVersion */);

    MainMenuButton lastButton = localGameButton;
    float gap = lastButton.getBoundingRect().height() * MENU_BUTTON_GAP_HEIGHT_RATIO;
    LayoutGroup group = new LayoutGroup()
        .add(localGameButton).atCurrentRect();

    if (Version.MULTIPLAYER.isEnabled()) {
      group.add(multiplayerButton)
          .relativeToEdge(localGameButton, LayoutGroup.Direction.BOTTOM, gap);
      lastButton = multiplayerButton;
    }
    group.add(replayButton).relativeToEdge(lastButton, LayoutGroup.Direction.BOTTOM, gap);

    group.setBoundingRect(new RectF(
        0,
        0,
        MENU_BUTTON_WIDTH_RATIO * canvasRect.width(),
        canvasRect.height()));
    group.offsetTo(
        0,
        MENU_BUTTON_BOTTOM_RATIO * canvasRect.height() - group.getBoundingRect().height());
  }

  @Override
  public void onUpdate(int elapsedTime) {
    animations.update(elapsedTime);
  }

  @Override
  public void onDraw(Canvas canvas) {
    title.draw(canvas);
    localGameButton.draw(canvas);
    replayButton.draw(canvas);
    menuPanel.draw(canvas);

    if (Version.MULTIPLAYER.isEnabled()) {
      multiplayerButton.draw(canvas);
    }
  }

  @Override
  public void onTouchEvent(MotionEvent event) {
    PointF touch = new PointF(event.getX(), event.getY());
    localGameButton.handleEvent(touch.x, touch.y, event.getAction());
    if (Version.MULTIPLAYER.isEnabled()) {
      multiplayerButton.handleEvent(touch.x, touch.y, event.getAction());
    }
    replayButton.handleEvent(touch.x, touch.y, event.getAction());
    menuPanel.handleEvent(touch.x, touch.y, event.getAction());
  }

  public void refreshPlayers() {
    for (int i = 0; i < playerButton.length; i++)
      playerButton[i].setPlayer(Settings.instance().getPlayer(i));
  }
}
